<template>
    <el-dialog
            :title="!dataForm.refundId ? '新增' : '修改'"
            :close-on-click-modal="false"
            :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
                            <el-form-item label="退费ID" prop="refundId">
                        <el-input v-model.number="dataForm.refundId" placeholder="退费ID"></el-input>
                </el-form-item>
                <el-form-item label="订单ID" prop="refundId">
                        <el-input v-model.number="dataForm.refundId" placeholder="订单ID"></el-input>
                </el-form-item>
                <el-form-item label="退费金额" prop="refundAmount">
                        <el-input v-model="dataForm.refundAmount" placeholder="退费金额"></el-input>
                </el-form-item>
                <el-form-item label="退费状态" prop="refundStatus">
                        <el-input v-model="dataForm.refundStatus" placeholder="退费状态"></el-input>
                </el-form-item>
                <el-form-item label="创建时间" prop="createTime">
                        <el-date-picker
                                v-model="dataForm.createTime"
                                type="datetime"
                                placeholder="创建时间">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="更新时间" prop="updateTime">
                        <el-date-picker
                                v-model="dataForm.updateTime"
                                type="datetime"
                                placeholder="更新时间">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="退费原因" prop="refundReason">
                        <el-input v-model="dataForm.refundReason" placeholder="退费原因"></el-input>
                </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
</template>

<script>
    export default {
      data () {
        return {
          visible: false,
          dataForm: {
            refundId: null,
            refundAmount: null,
            refundStatus: null,
            createTime: null,
            updateTime: null,
            refundReason: null
          },
          dataRule: {
            refundId: [
                        { required: true, message: '退费ID不能为空', trigger: 'blur' }
            ],
            refundAmount: [
                        { required: true, message: '退费金额不能为空', trigger: 'blur' }
            ],
            refundStatus: [
                        { required: true, message: '退费状态不能为空', trigger: 'blur' }
            ],
            createTime: [
                        { required: true, message: '创建时间不能为空', trigger: 'blur' }
            ],
            updateTime: [
                        { required: true, message: '更新时间不能为空', trigger: 'blur' }
            ],
            refundReason: [
                        { required: true, message: '退费原因不能为空', trigger: 'blur' }
            ]
          }
        }
      },
      methods: {
        init (id) {
          this.dataForm.refundId = id || 0
          this.visible = true
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
            this.dataForm.refundId = id || 0
            if (this.dataForm.refundId) {
              this.$http({
                url: this.$http.adornUrl(`/app/refund/info/${this.dataForm.refundId}`),
                method: 'get',
                params: this.$http.adornParams()
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.dataForm = data.apprefund
                }
              })
            }
          })
        },
            // 表单提交
        dataFormSubmit () {
          this.$refs['dataForm'].validate((valid) => {
            if (valid) {
              this.$http({
                url: this.$http.adornUrl(`/app/refund/${!this.dataForm.refundId ? 'save' : 'update'}`),
                method: 'post',
                data: this.$http.adornData(this.dataForm)
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                      this.visible = false
                      this.$emit('refreshDataList')
                    }
                  })
                } else {
                  this.$message.error(data.msg)
                }
              })
            }
          })
        }
      }
    }
</script>